package app.project.directionlensproject;

import java.util.Locale;

import com.google.android.gms.maps.model.LatLng;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import app.project.directionlensproject.fragment.FgCategory;
import app.project.directionlensproject.fragment.FgEditUser;
import app.project.directionlensproject.fragment.FgLogin;
import app.project.directionlensproject.fragment.FgRegister;
import app.project.directionlensproject.fragment.FgResetPass1;
import app.project.directionlensproject.fragment.FgResetPass2;
import app.project.directionlensproject.util.BundleKeys;
import app.project.directionlensproject.util.CommonUtils;
import app.project.directionlensproject.util.Constants;
import app.project.directionlensproject.util.DialogUtils;
import app.project.directionlensproject.util.Utilities;

public class HomeActivity extends GeneralActivity {
	public static int TYPE_HOME = 1; // 1-> cate, 2-> login, 3-> change pass
	public static final int TYPE_LOGIN = 2;
	public static final int TYPE_CATE = 1;
	public static final int TYPE_CHANGEPASS = 3;
	public TextView tvUserName;
	public boolean isLogin;
	public static double MY_LATITUDE;
	public static double MY_LONGITUDE;
	public boolean isCategoryFg;
	public ImageButton btnVietNam;
	public ImageButton btnEnglish;
	public static boolean isListAll=false;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		boolean isEn = CommonUtils.getBooleanSetting(this, Constants.IS_EN, false);
		if (!isEn) {
			Configuration c = new Configuration(getResources().getConfiguration());
			c.locale = new Locale("vi");
			getResources().updateConfiguration(c, getResources().getDisplayMetrics());
		} else {
			Configuration c = new Configuration(getResources().getConfiguration());
			c.locale = Locale.ENGLISH;
			getResources().updateConfiguration(c, getResources().getDisplayMetrics());
		}
		setContentView(R.layout.activity_home);

		LatLng myPlace = Utilities.getInstance().getMyPlace(this);
		//public static final LatLng FPT = new LatLng(21.029151, 105.782222);
		MY_LATITUDE = myPlace.latitude;
		MY_LONGITUDE = myPlace.longitude;
		
		Log.e("Myplace",MY_LATITUDE+"/"+MY_LONGITUDE);
		
		tvUserName = (TextView) findViewById(R.id.tv_name);
		btnEnglish = (ImageButton) findViewById(R.id.btn_en);
		btnVietNam = (ImageButton) findViewById(R.id.btn_viet);
		initData();
		loginIfNeed();
	}
	
	public void loginIfNeed(){
		String tokenID = CommonUtils.getStringSetting(this, Constants.TOKEN_ID, null);
		String userName = CommonUtils.getStringSetting(this, Constants.USER_NAME, null);
		if(tokenID!=null && userName!=null){
			tvUserName.setText(getString(R.string.hello) + " "+userName);
			Log.e("TokenID",tokenID);
			isLogin = true;
		}
	}

	public void initData() {
		View llUser = findViewById(R.id.layout_user);
		llUser.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (isLogin) {
					DialogUtils.showDialogUser(HomeActivity.this, new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							if (which == 0) {// my place
								Intent i = new Intent(HomeActivity.this, ListPlaceActivity.class);
								i.putExtra(BundleKeys.IS_USER_PLACE, true);
								MyApplication.isListForAll=false;
								HomeActivity.isListAll=false;
								startActivity(i);
							} else if (which == 1) {// change pass
								gotoEditUser();
							} else {// logout
								isLogin = false;
								tvUserName.setText(getString(R.string.hello_gues));
								CommonUtils.saveStringSetting(HomeActivity.this, Constants.TOKEN_ID, null);
								CommonUtils.saveStringSetting(HomeActivity.this, Constants.USER_NAME, null);
								Toast.makeText(getApplicationContext(), R.string.logout_sucess, Toast.LENGTH_LONG)
										.show();
							}
						}
					});

				} else
					gotoLogin();
			}
		});

		btnVietNam.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				Configuration c = new Configuration(getResources().getConfiguration());
				c.locale = new Locale("vi");
				getResources().updateConfiguration(c, getResources().getDisplayMetrics());
				CommonUtils.saveBooleanSetting(getApplicationContext(), Constants.IS_EN, false);
				Intent intent = getIntent();
				overridePendingTransition(0, 0);
				intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				finish();
				overridePendingTransition(0, 0);
				startActivity(intent);
			}
		});
		btnEnglish.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				Configuration c = new Configuration(getResources().getConfiguration());
				c.locale = Locale.ENGLISH;
				getResources().updateConfiguration(c, getResources().getDisplayMetrics());
//				Log.d("a", "en");
				CommonUtils.saveBooleanSetting(getApplicationContext(), Constants.IS_EN, true);
				Intent intent = getIntent();
				overridePendingTransition(0, 0);
				intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
				finish();
				overridePendingTransition(0, 0);
				startActivity(intent);
			}
		});
	}

	@Override
	protected void onResume() {
		if (TYPE_HOME == TYPE_CHANGEPASS)
			gotoEditUser();
		else if (TYPE_HOME == TYPE_LOGIN)
			gotoLogin();
		else
			gotoCategory();
		TYPE_HOME = 1;
		super.onResume();
	}

	public void gotoCategory() {
		isCategoryFg = true;
		Fragment fg = new FgCategory();
		getSupportFragmentManager().beginTransaction().replace(R.id.content_layout, fg).commit();
	}

	public void gotoLogin() {
		isCategoryFg = false;
		Fragment fg = new FgLogin();
		getSupportFragmentManager().beginTransaction().replace(R.id.content_layout, fg).commit();
	}

	public void gotoRegister() {
		isCategoryFg = false;
		Fragment fg = new FgRegister();
		getSupportFragmentManager().beginTransaction().replace(R.id.content_layout, fg).commit();
	}

	public void gotoEditUser() {
		isCategoryFg = false;
		Fragment fg = new FgEditUser();
		getSupportFragmentManager().beginTransaction().replace(R.id.content_layout, fg).commit();
	}

	public void gotoResetPass1() {
		isCategoryFg = false;
		Fragment fg = new FgResetPass1();
		getSupportFragmentManager().beginTransaction().replace(R.id.content_layout, fg).commit();
	}

	public void gotoResetPass2() {
		isCategoryFg = false;
		Fragment fg = new FgResetPass2();
		getSupportFragmentManager().beginTransaction().replace(R.id.content_layout, fg).commit();
	}

	@Override
	public void onBackPressed() {
		if (isCategoryFg) {
			DialogUtils.showQuest(this, R.string.confirm, R.string.confirm_quit_mes, R.string.ok, R.string.cancel,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
//							CommonUtils.saveStringSetting(getApplicationContext(), Constants.TOKEN_ID, null);
//							CommonUtils.saveStringSetting(getApplicationContext(), Constants.USER_NAME, null);
							CommonUtils.saveStringSetting(getApplicationContext(), Constants.EMAIL, null);
							FgCategory.items = null;
							HomeActivity.this.finish();
							Intent startMain = new Intent(Intent.ACTION_MAIN);
							startMain.addCategory(Intent.CATEGORY_HOME);
							startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
							startActivity(startMain);
						}
					}, null);
		} else {
			gotoCategory();
		}

	}
}
